﻿using AutoMapper;
using BaseData.API.Write.Applications.MCI.Commands;
using BaseData.Domain.MCI;
using BaseData.ErrorCode;
using BaseData.Infrastructure.Interfaces;
using MediatR;

namespace BaseData.API.Write.Applications.MCI.CommandHandlers
{
    /// <summary>
    /// 添加就诊人信息-命令
    /// </summary>
    public class AddPatientCommandHandler : IRequestHandler<AddPatientCommand, ApiResult<int>>
    {
        /// <summary>
        /// 就诊人信息仓储
        /// </summary>
        private readonly IBaseRepository<Patient> patientRep;
        private readonly IMapper mapper;

        public AddPatientCommandHandler(IBaseRepository<Patient> patientRep,
            IMapper mapper)
        {
            this.patientRep = patientRep;
            this.mapper = mapper;
        }
        public async Task<ApiResult<int>> Handle(AddPatientCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<int> result = new ApiResult<int>();
                result.Code = ApiEnum.添加成功;
                result.Message= ApiEnum.添加成功.ToString();

                var entity=mapper.Map<Patient>(request);
                await patientRep.AddAsync(entity);
                result.Data = 1;
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
